package com.eascs.admin.o2o.detail.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.admin.o2o.detail.form.PageForm;
import com.eascs.admin.o2o.detail.form.SearchForm;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UuidUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.site.view.button.RedirectButton;

@RestController
public class HtmlRestController extends BaseRestController {
	public final Logger logger = LoggerFactory.getLogger(HtmlRestController.class);

	@RequestMapping(value = "/demo/html/pagination")
	public PaginationRestResult paginationRestResult(PageForm pageForm) {
		String url = "/demo/html/pagination?pageIndex=";
		PageResult<Object[]> pageResult = null;
		if (null == pageForm.getPageIndex()) {
			pageResult = new PageResult<>(10, 0, 0);
		} else {
			pageResult = new PageResult<>(10, pageForm.getPageIndex(), 10);
			List<Object[]> data = new ArrayList<>();
			data.add(new Object[] { "1", "o2o-001", "张三" + System.currentTimeMillis() });
			data.add(new Object[] { "2", "o2o-002", "李四" });
			data.add(new Object[] { "1", "o2o-001", "张三" });
			data.add(new Object[] { "2", "o2o-002", "李四" });
			data.add(new Object[] { "1", "o2o-001", "张三" });
			data.add(new Object[] { "2", "o2o-002", "李四" });
			data.add(new Object[] { "1", "o2o-001", "张三" });
			data.add(new Object[] { "2", "o2o-002", "李四" });
			data.add(new Object[] { "1", "o2o-001", "张三" });
			data.add(new Object[] { "2", "o2o-002", "李四" });
			data.add(new Object[] { "1", "o2o-001", "张三" });
			data.add(new Object[] { "2", "o2o-002", "李四" });
			data.add(new Object[] { "1", "o2o-001", "张三" });
			pageResult.setData(data);
		}

		String[] titles = { "客户号", "客户姓名" };
		Button[] moreButtons = new Button[] { ButtonHelper.createAjaxButton("删除", "/delete?id="), ButtonHelper.createRedirectButton("修改", "/update?id=") };
		Button[] titleButtons = new Button[] { new RedirectButton("查看", "/update?id="), null };
		String rendTo = "#pagination";
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, url, rendTo);
		paginationInfo.setMoreButtons(moreButtons);
		paginationInfo.setTitleButtons(titleButtons);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}

	@RequestMapping(value = "/demo/html/table")
	public PaginationRestResult table() {
		PaginationInfo paginationInfo = buildPaginationInfo();
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}

	public static PaginationInfo buildPaginationInfo() {
		String url = "/demo/html/pagination?pageIndex=";
		PageResult<Object[]> pageResult = new PageResult<>(10, 0, 10);
		List<Object[]> data = new ArrayList<>();
		data.add(new Object[] { "1", "o2o-001", "张三" });
		data.add(new Object[] { "2", "o2o-002", "李四" });
		data.add(new Object[] { "1", "o2o-001", "张三" });
		data.add(new Object[] { "2", "o2o-002", "李四" });
		data.add(new Object[] { "1", "o2o-001", "张三" });
		data.add(new Object[] { "2", "o2o-002", "李四" });
		data.add(new Object[] { "1", "o2o-001", "张三" });
		data.add(new Object[] { "2", "o2o-002", "李四" });
		data.add(new Object[] { "1", "o2o-001", "张三" });
		data.add(new Object[] { "2", "o2o-002", "李四" });
		data.add(new Object[] { "1", "o2o-001", "张三" });
		data.add(new Object[] { "2", "o2o-002", "李四" });
		data.add(new Object[] { "1", "o2o-001", "张三" });
		pageResult.setData(data);

		String[] titles = { "客户号", "客户姓名" };
		Button[] moreButtons = new Button[] { ButtonHelper.createAjaxConfirmButton("删除", "/delete?id="), ButtonHelper.createRedirectButton("修改", "/update?id=") };
		Button[] titleButtons = new Button[] { ButtonHelper.createRedirectButton("查看", "/update?id="), null };
		String rendTo = "#pagination";
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, url, rendTo);
		paginationInfo.setMoreButtons(moreButtons);
		paginationInfo.setTitleButtons(titleButtons);
		paginationInfo.setBuildPagingNavigation(false);
		return paginationInfo;
	}

	@RequestMapping(value = "/demo/html/search")
	public BaseRestResult<List<Object[]>> search(SearchForm searchForm) {
		List<Object[]> data = new ArrayList<>();
		if (StringUtil.hasText(searchForm.getKey())) {
			data.add(new Object[] { UuidUtil.generateShortUuid(), searchForm.getKey() + UuidUtil.generateShortUuid() });
			data.add(new Object[] { UuidUtil.generateShortUuid(), searchForm.getKey() + UuidUtil.generateShortUuid() });
			data.add(new Object[] { UuidUtil.generateShortUuid(), searchForm.getKey() + UuidUtil.generateShortUuid() });
			data.add(new Object[] { UuidUtil.generateShortUuid(), searchForm.getKey() + UuidUtil.generateShortUuid() });
		}
		return BaseRestResult.buildRestResult(data);
	}

	@RequestMapping(value = "/demo/html/search-desc")
	public BaseRestResult<List<Object[]>> searchWithWDesc(SearchForm searchForm) {
		List<Object[]> data = new ArrayList<>();
		if (StringUtil.hasText(searchForm.getKey())) {
			data.add(new Object[] { UuidUtil.generateShortUuid(), searchForm.getKey() + UuidUtil.generateShortUuid(), "desc:" + searchForm.getKey() + UuidUtil.generateShortUuid() });
			data.add(new Object[] { UuidUtil.generateShortUuid(), searchForm.getKey() + UuidUtil.generateShortUuid(), "desc:" + searchForm.getKey() + UuidUtil.generateShortUuid() });
			data.add(new Object[] { UuidUtil.generateShortUuid(), searchForm.getKey() + UuidUtil.generateShortUuid(), "desc:" + searchForm.getKey() + UuidUtil.generateShortUuid() });
		}
		return BaseRestResult.buildRestResult(data);
	}

}